//index.js
//获取应用实例
var app = getApp()
var auth = require('../../lib/auth/login')
var tools = require('../../lib/system/tools')
var pages = require('../../lib/common/pageConstant')
var commonConstant = require('../../lib/common/constant')
var businessStorage = require('../../lib/common/storage')


Page({
    data: {
        motto: 'Hello World',
        userInfo: {},
        hasUserInfo: false,
        canIUse: wx.canIUse('button.open-type.getUserInfo'),
        commonConstant: commonConstant,
        productList: [
            {
                name: 'name',
                description: 'desc',
                id: '123',
                photo: '../../resources/logo.png',
                price: 123,
                next_price: 23
            }
        ],
        currentGoods: {},
        tabbarType: {
            type: 'index'
        }
    },
    //事件处理函数
    bindViewTap: function () {
        wx.navigateTo({
            url: '../logs/logs'
        })
    },
    onShow: function () {
        var that = this;
        tools.requestWithAuth({
            url: 'product',
            feedback: {
                success: data => {
                    console.log(data)
                    that.setData({
                        productList: data
                    })
                }
            }
        })
    },
    buyGoods: function (e) {
        var productId = e.currentTarget.dataset.product_id
        var detailPage = pages.productDetail + '?product_id=' + productId
        var productList = this.data.productList
        var currentGoods = {}
        for (var k in productList) {
            if (productList[k]['id'] == productId) {
                currentGoods = productList[k]
            }
        }
        businessStorage.save('current_pick_goods', currentGoods)
        wx.navigateTo({
            url: detailPage
        })
    }
})
